package org.jeecg.modules.cascv.image.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.file.LineSeparator;
import com.alibaba.fastjson.JSONObject;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.cascv.image.service.merge.GaoweiguiService;
import org.jeecg.modules.cascv.image.service.merge.WurongqiService;
import org.jeecg.modules.cascv.image.service.merge.WurongqiweikongService;
import org.jeecg.modules.cascv.image.util.LaptopConstant;
import org.jeecg.modules.cascv.image.util.MergeUtil;
import org.jeecg.modules.message.websocket.WebSocket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

@Service
public class MergeVideoService {

    @Resource
    private WebSocket webSocket;
    @Value("${jeecg.path.upload}")
    private String upLoadPath;

    public Result<Object> merge(JSONObject jsonObject, String fileId, String userId) {
        String tableName = jsonObject.getString("tableName");
        if(StringUtils.isEmpty(fileId)){
            fileId = tableName;
        }
        if(StringUtils.equalsIgnoreCase(tableName, fileId)){
            LaptopConstant.idsFileTableName.put(tableName,"");
        }

        LaptopConstant.timeMap.put(fileId, System.currentTimeMillis() + 1000 * 10);

        upLoadPath = "d://files//";

        Long pt = LaptopConstant.ptCache.getIfPresent(tableName);
        if(pt == null){
            LaptopConstant.ptCache.put(tableName, 1L);
            FileUtil.del(upLoadPath + "/video/" + tableName);
            LaptopConstant.fileIndexCache.remove(tableName);
            LaptopConstant.secondIndexCache.invalidate(tableName);
        }else{
            LaptopConstant.ptCache.put(tableName, pt);
        }

        Integer second = jsonObject.getInteger("second");//todo
        Integer subsecond = jsonObject.getInteger("subsecond");//todo
        Long ts = second * 100000L + subsecond;


        File file = FileUtil.appendUtf8String(jsonObject.toString() + System.lineSeparator(), upLoadPath + "/video/" + tableName);
        LaptopConstant.filePathMap.put(tableName, upLoadPath + "/video/" + tableName);

        Cache<Integer, Long> indexCache = LaptopConstant.fileIndexCache.get(tableName);
        if(indexCache == null){
            synchronized (LaptopConstant.fileIndexCache){
                indexCache = LaptopConstant.fileIndexCache.get(tableName);
                if(indexCache == null){
                    indexCache = Caffeine.newBuilder().build();
                    LaptopConstant.fileIndexCache.put(tableName, indexCache);
                }
            }
        }

        indexCache.put(second, file.length());

        LinkedList<String> secondIndexCache = LaptopConstant.secondIndexCache.getIfPresent(tableName);
        if(secondIndexCache == null){
            synchronized (LaptopConstant.secondIndexCache){
                secondIndexCache = LaptopConstant.secondIndexCache.getIfPresent(tableName);
                if(secondIndexCache == null){
                    secondIndexCache = new LinkedList<>();
                    LaptopConstant.secondIndexCache.put(tableName, secondIndexCache);
                }
            }
        }

        secondIndexCache.add(second + "," + second);
        if(secondIndexCache.size() > 20){
//            String s1 = CollectionUtil.join(secondIndexCache, "#");
            Long[][] longs = MergeUtil.mergeRange(secondIndexCache, 10);
            secondIndexCache = new LinkedList<>();
            for (Long[] aLong : longs) {
                secondIndexCache.add(aLong[0] + "," + aLong[1]);
            }
            LaptopConstant.secondIndexCache.put(tableName, secondIndexCache);
//            String s2 = CollectionUtil.join(secondIndexCache, "#");
//            FileUtil.appendUtf8Lines(Arrays.asList(s1,s2), "D:\\files\\video\\index-merge-" + tableName + ".txt");
        }

        return null;
    }


}
